package thread.queue.BlockingQueue;

import thread.queue.Consumer;
import thread.queue.Producer;
import thread.queue.Store;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created by xu on 2021/12/5.
 */
public class StoreBlockQueue implements Store {
    private BlockingQueue<String> queue = new LinkedBlockingQueue(10);

    @Override
    public void produce(String s) throws InterruptedException {
        Thread.sleep(200);
        queue.put(s);
        System.out.println("生产资源:"+Thread.currentThread().getId()+",param:"+s +",size:"+queue.size());

    }

    @Override
    public void consume() throws InterruptedException {
        String t = queue.take();
        Thread.sleep(1000);
        System.out.println("消费资源："+t+",size:"+queue.size());
    }


    public static void main(String[] args) {
        Store store = new StoreBlockQueue();

        Producer p1 =  new Producer(store);
        Producer p2 =  new Producer(store);
        Producer p3 =  new Producer(store);
        new  Thread(p1).start();
        new  Thread(p2).start();
        new  Thread(p3).start();
        Consumer c1 =  new Consumer(store);
        new  Thread(c1).start();

    }

}
